package edu.avans.hartigehap.web.frontend.controller;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import edu.avans.hartigehap.domain.*;
import edu.avans.hartigehap.domain.decorator.AbstractMenuItem;
import edu.avans.hartigehap.domain.decorator.MenuItemWithIngredient;
import edu.avans.hartigehap.domain.decorator.TestIngredient;
import edu.avans.hartigehap.repository.MenuItemRepository;
import edu.avans.hartigehap.service.*;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("restaurants/{restaurantName}")
@Controller
public class IngredientFrontendController {
		
	final Logger logger = LoggerFactory.getLogger(IngredientFrontendController.class);

	@Autowired
	private RestaurantService restaurantService;
	@Autowired
	private BillService billService;
	@Autowired
	private IngredientService ingredientService;
	
	@Autowired
	private TestIngredientService testIngredientService;
	
	@Autowired
	private MenuItemRepository menuItemRepository;

	// mapping to "/" is not RESTful, but is for bootstrapping!
	@RequestMapping(value = {"ingredients"}, method = RequestMethod.GET)
	public String listIngredients(
			Model uiModel, 
			@PathVariable("restaurantName") String restaurantName
			) {
		//Collection<Restaurant> restaurants = restaurantService.findAll();
		//uiModel.addAttribute("restaurants", restaurants);

		// use HartigeHap as default restaurant
		// bad practice use constant from serviceImpl (TODO)
		//Restaurant restaurant = restaurantService.fetchWarmedUp(RestaurantServiceImpl.hartigeHapRestaurantName);
		//uiModel.addAttribute("restaurant", restaurant);
		
		return "frontend/hartigehap/ingredients";
	}
	

	@RequestMapping(value = "{menuItemName}/ingredients", method = RequestMethod.POST)
	public String viewIngredients(
			Model uiModel, 
			
			@PathVariable("restaurantName") String restaurantName,
			@PathVariable("menuItemName") String menuItemName
			//@RequestParam String menuItemName
			) {
		
		Collection<TestIngredient> ingredients = testIngredientService.findAll();
		uiModel.addAttribute("ingredients", ingredients);
		
		//Collection<Ingredient> ingredients = ingredientService.findAll();
		//uiModel.addAttribute("ingredients", ingredients);
		
		uiModel.addAttribute("menuItemName", menuItemName);
		uiModel.addAttribute("restaurantName", restaurantName);
		// use HartigeHap as default restaurant
		// bad practice use constant from serviceImpl (TODO)
		//Restaurant restaurant = restaurantService.fetchWarmedUp(RestaurantServiceImpl.hartigeHapRestaurantName);
		//uiModel.addAttribute("restaurant", restaurant);
		
		
		/*logger.info("ingredientName = " + ingredientName);
		
		uiModel.addAttribute("ingredientName", ingredientName);*/

		
		return "frontend/hartigehap/ingredients";
	}
	
	
	@RequestMapping(value = "/{menuItemName}/ingredients", method = RequestMethod.GET)
	public String viewIngredientsMenu(
			Model uiModel, 
			@PathVariable("restaurantName") String restaurantName,
			
			@PathVariable("menuItemName") String menuItemName
			//@RequestParam String menuItemName, Model uiModel
			) {
		
		//Collection<Ingredient> ingredients = ingredientService.findAll();
		
		//uiModel.addAttribute("ingredients", ingredients);
		
		Collection<TestIngredient> ingredients = testIngredientService.findAll();
		uiModel.addAttribute("ingredients", ingredients);
		
		uiModel.addAttribute("menuItemName", menuItemName);
		uiModel.addAttribute("restaurantName", restaurantName);
		
		// use HartigeHap as default restaurant
		// bad practice use constant from serviceImpl (TODO)
		//Restaurant restaurant = restaurantService.fetchWarmedUp(RestaurantServiceImpl.hartigeHapRestaurantName);
		//uiModel.addAttribute("restaurant", restaurant);
		
		
		/*logger.info("ingredientName = " + ingredientName);
		
		uiModel.addAttribute("ingredientName", ingredientName);*/

		
		return "frontend/hartigehap/ingredients";
	}
	
	@RequestMapping(value = "{menuItemName}/ingredients", method = RequestMethod.PUT)
	public String saveIngredientsMenu(
			Model uiModel, 
			@PathVariable("restaurantName") String restaurantName,
			
			@PathVariable("menuItemName") String menuItemName
			//@RequestParam String menuItemName, Model uiModel
			) {
		return "redirect:/restaurants/"+restaurantName+"/"+menuItemName+"/ingredients";
	}

	@RequestMapping(value = "{menuItemName}/ingredients/{ingredientName}", method = RequestMethod.POST)
	public String addIngredients(
			HttpServletRequest request,
			Model uiModel, 
			
			@PathVariable("restaurantName") String restaurantName,
			@RequestParam String ingredientName, String menuItemName) throws StateException {
		
		logger.info("ingredientName = " + ingredientName);
		
		MenuItem mi = menuItemRepository.findOne(menuItemName);
		TestIngredient ti = testIngredientService.findById(ingredientName);
		
		MenuItemWithIngredient concreteDecorator = new MenuItemWithIngredient();
		concreteDecorator.setAbstractMenuItem(mi);
		concreteDecorator.setTestIngredient(ti);
		
		logger.info("decorated price" + concreteDecorator.getPrice());
		//logger.info("Ami price: " + ami.getPrice());
		
		Long billId = (Long) request.getAttribute("billId");
		logger.info("billId = " + billId);
		Bill bill = billService.findBillById(billId);
		logger.info("bill = " + bill);
		
		//billService.addOrderItem(bill, ingredientName);
		
		//billService.addOrderItem(bill, ami);
		billService.addOrderItemIngredient(bill, ti, mi);
		
		// TODO Hier moet nog het ID mee worden gegeven van de pizza, om terug te gaan naar de lijst met ingredienten
		return "redirect:/restaurants/"+restaurantName+"/"+menuItemName+"/ingredients";
	}
	
	@RequestMapping(value = "{menuItemName}/ingredients/{ingredientName}", method = RequestMethod.DELETE)
	public String deleteIngredients(
			Model uiModel, 
			
			@PathVariable("restaurantName") String restaurantName,
			@RequestParam String ingredientName, String menuItemName
			) {

		logger.info("ingredientName = " + ingredientName);
		
		//uiModel.addAttribute("ingredientName", ingredientName);
		//uiModel.addAttribute("menuItemName", menuItemName);
		
		// TODO Hier moet nog het ID mee worden gegeven van de pizza, om terug te gaan naar de lijst met ingredienten
		return "redirect:/restaurants/"+restaurantName+"/"+menuItemName+"/ingredients";
	}
}
